package com.cnap.multiclouldmgmt.model.flavor;

import com.cnap.multiclouldmgmt.service.impl.VmResourceServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 云规格信息
 * 
 * @author tyj
 * @date 2022/11/22
 */
public class CloudFlavorInfoV2 {

    private final static Logger LOGGER = LoggerFactory.getLogger(CloudFlavorInfoV2.class);


    private String accountName;

    /**
     * 云类型
     */
    private String cloudType;
    /**
     * 云区域
     */
    private String regionId;

    /**
     * 云区域描述
     */
    private String regionDescribe;
    /**
     * 可用区
     */
    private String zoneId;
    /**
     * 可用区描述
     */
    private String zoneDescribe;
    /**
     * 大区位置
     */
    private String regionLocation;

    /**
     * 实例规格云商ID
     */
    private String flavorId;
    /**
     * 实例规格描述
     */
    private String flavorDescribe;

    /**
     * 镜像信息
     */
    private String imageStr;

    /**
     * 硬盘信息
     */
    private String diskTypeStr;

    /**
     * 实体编号（唯一标识）
     */
    protected int id;
    /**
     * 创建时间
     */
    private Date createDate;
    /**
     * 更新时间
     */
    private Date updateDate;

    /**
     * get
     *
     * @return AccountName
     */
    public String getAccountName() {
        return accountName;
    }

    /**
     * set
     *
     * @param accountName accountName
     */
    public void setAccountName(String accountName) {
        this.accountName = accountName;
    }

    /**
     * get
     *
     * @return CloudType
     */
    public String getCloudType() {
        return cloudType;
    }

    /**
     * set
     *
     * @param cloudType cloudType
     */
    public void setCloudType(String cloudType) {
        this.cloudType = cloudType;
    }

    /**
     * get
     *
     * @return RegionId
     */
    public String getRegionId() {
        return regionId;
    }

    /**
     * set
     *
     * @param regionId regionId
     */
    public void setRegionId(String regionId) {
        this.regionId = regionId;
    }

    /**
     * get
     *
     * @return RegionDescribe
     */
    public String getRegionDescribe() {
        return regionDescribe;
    }

    /**
     * set
     *
     * @param regionDescribe regionDescribe
     */
    public void setRegionDescribe(String regionDescribe) {
        this.regionDescribe = regionDescribe;
    }

    /**
     * get
     *
     * @return ZoneId
     */
    public String getZoneId() {
        return zoneId;
    }

    /**
     * set
     *
     * @param zoneId zoneId
     */
    public void setZoneId(String zoneId) {
        this.zoneId = zoneId;
    }

    /**
     * get
     *
     * @return ZoneDescribe
     */
    public String getZoneDescribe() {
        return zoneDescribe;
    }

    /**
     * set
     *
     * @param zoneDescribe zoneDescribe
     */
    public void setZoneDescribe(String zoneDescribe) {
        this.zoneDescribe = zoneDescribe;
    }

    /**
     * get
     *
     * @return RegionLocation
     */
    public String getRegionLocation() {
        return regionLocation;
    }

    /**
     * set
     *
     * @param regionLocation regionLocation
     */
    public void setRegionLocation(String regionLocation) {
        this.regionLocation = regionLocation;
    }

    /**
     * get
     *
     * @return FlavorId
     */
    public String getFlavorId() {
        return flavorId;
    }

    /**
     * set
     *
     * @param flavorId flavorId
     */
    public void setFlavorId(String flavorId) {
        this.flavorId = flavorId;
    }

    /**
     * get
     *
     * @return FlavorDescribe
     */
    public String getFlavorDescribe() {
        return flavorDescribe;
    }

    /**
     * set
     *
     * @param flavorDescribe flavorDescribe
     */
    public void setFlavorDescribe(String flavorDescribe) {
        this.flavorDescribe = flavorDescribe;
    }

    /**
     * get
     *
     * @return ImageStr
     */
    public String getImageStr() {
        return imageStr;
    }

    /**
     * set
     *
     * @param imageStr imageStr
     */
    public void setImageStr(String imageStr) {
        this.imageStr = imageStr;
    }

    /**
     * get
     *
     * @return DiskTypeStr
     */
    public String getDiskTypeStr() {
        return diskTypeStr;
    }

    /**
     * set
     *
     * @param diskTypeStr diskTypeStr
     */
    public void setDiskTypeStr(String diskTypeStr) {
        this.diskTypeStr = diskTypeStr;
    }

    /**
     * get
     *
     * @return Id
     */
    public int getId() {
        return id;
    }

    /**
     * set
     *
     * @param id id
     */
    public void setId(int id) {
        this.id = id;
    }

    /**
     * get
     *
     * @return CreateDate
     */
    public Date getCreateDate() {
        return createDate;
    }

    /**
     * set
     *
     * @param createDate createDate
     */
    public void setCreateDate(Date createDate) {
        this.createDate = createDate;
    }

    /**
     * get
     *
     * @return UpdateDate
     */
    public Date getUpdateDate() {
        return updateDate;
    }

    /**
     * set
     *
     * @param updateDate updateDate
     */
    public void setUpdateDate(Date updateDate) {
        this.updateDate = updateDate;
    }

    //解析出cpu和mem大小，格式长这样c7.xlarge.2[4Cores][8GB]
    public Map<String,Integer> handdleCpuAndMem(){
        Map<String,Integer> resMap = new HashMap<>();
        try {
            String cpuAndMem =
                    flavorDescribe
                            .replace(flavorId, "")
                            .replace(".0", "");
            String[] nums = cpuAndMem.split("ores");
            String regEx = "[^0-9]";
            Pattern p = Pattern.compile(regEx);
            //正则表达式获取数字
            for (String num : nums) {
                Matcher m = p.matcher(num);

                if (!resMap.containsKey("cpu")) {
                    resMap.put("cpu",Integer.parseInt(m.replaceAll("").trim()));
                    continue;
                }

                resMap.put("mem",Integer.parseInt(m.replaceAll("").trim()));
            }
        }catch (Exception e){

            LOGGER.error("handdleCpuAndMem flavorInfo fail id={} describe={}",flavorId,flavorDescribe);
        }

        return resMap;
    }


}
